package com.lei.demo.controller;

import com.lei.demo.entity.TripForm;
import com.lei.demo.entity.User;
import com.lei.demo.service.TripFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Shinelon on 2017/6/15.
 */
@Controller
@RequestMapping("/tripform")
public class TripFormController {

    @Autowired
    private TripFormService tripFormService;

    @RequestMapping(value = "/handup",method = {RequestMethod.GET,RequestMethod.POST})
    public String handUpTripForm(Model model, TripForm tripForm,HttpServletRequest request) {
        model.addAttribute("status","exist");
        User user = (User)request.getSession().getAttribute("user");
        tripForm.setUsername(user.getName());
        tripFormService.save(tripForm);
        model.addAttribute("saved","success");
        return "TripForm_new";
    }

    @RequestMapping(value = "/increase", method = {RequestMethod.GET,RequestMethod.POST})
    public String increaseTripForm(Model model, TripForm tripForm) {
        model.addAttribute("status", "exist");
        return "TripForm_new";
    }

    @RequestMapping(value = "/budget", method = {RequestMethod.GET,RequestMethod.POST})
    public String budget(Model model,TripForm tripForm) {
        model.addAttribute("status", "exist");
        return "budget_new";
    }

    @RequestMapping(value = "/total", method = {RequestMethod.GET,RequestMethod.POST})
    public String totalForm(Model model,TripForm tripForm) {
        model.addAttribute("status", "exist");
        return "TotalForm_new";
    }

    @RequestMapping(value = "/trip", method = {RequestMethod.GET,RequestMethod.POST})
    public String tripForm(Model model, TripForm tripForm) {
        return "TripForm_new";
    }

    @RequestMapping(value = "/update", method = {RequestMethod.GET,RequestMethod.POST})
    public String updateTripForm(Model model, TripForm tripForm, HttpServletRequest request) {
        tripFormService.update(tripForm);
        model.addAttribute("saved", "success");
        return "TripForm_check";
    }

    @RequestMapping(value = "/check", method = {RequestMethod.GET,RequestMethod.POST})
    public String checkTripForm(Model model,HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        List<TripForm> tripForms = tripFormService.findTripForms(user.getName());
        model.addAttribute("tfs", tripForms);
        return "TripForm_check";
    }

    @RequestMapping(value = "delete/{trid}", method = {RequestMethod.GET,RequestMethod.POST})
    public String deleteTripForm(Model model, @PathVariable int trid) {
        List<TripForm> tripForms = tripFormService.findTripForms(trid);
        model.addAttribute("tripforms", tripForms);
        tripFormService.delete(trid);
        model.addAttribute("deleted", "success");
        model.addAttribute("tripform", new TripForm());
        return "TripForm_check";
    }


    @RequestMapping(value = "edit/{trid}", method = {RequestMethod.GET,RequestMethod.POST})
    public String updateTripForm(Model model, @PathVariable int trid) {
        TripForm tripForm = tripFormService.findById(trid);
        model.addAttribute("tripform", tripForm);
        return "TripForm_edit";
    }

}
